const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ios:app.globalData.ios,//是否为ios
    id: '', //商品id,
    items: '',
    address: '',
    flage: false, //是否选中
    content: '', //备注内容
    // domain:Api.domain,
    jf:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    if(app.globalData.ios){
      this.setData({
        flage:true
      })
    }
    this.init()
  },
  init() { //商品详情
    var that = this
    var params = {
      goods_id: this.data.id
    }
    wx.$http.get(wx.$get.goods_info,params).then(res=>{
      console.log('商品详情', res)
      that.setData({
        items: res,
      })
    })
  },
  black() { //返回
    wx.navigateBack({
      delta: 1,
    })
  },
  moren() { //默认地址
    var that = this
    wx.$http.get(wx.$get.default_goods_address,{}).then(res=>{
      console.log('默认地址', res)
      that.setData({
        address: res
      })
    })
  },
  godetail() { //去地址列表
    wx.navigateTo({
      url: '/pages/shippingAddress/shippingAddress',
    })
  },
  qie() {
    this.setData({
      flage: !this.data.flage
    })
  },
  bindchinge(e) {
    this.setData({
      content: e.detail.value
    })
  },
  zhi() { //立即支付
    if (this.data.items.type == 1 && this.data.address) {
      this.fukuan()
    } else if (this.data.items.type != 1) {
      this.fukuan()
    } else {
      wx.navigateTo({
        url: '/pages/shippingAddress/shippingAddress',
      })
    }
  },
  fukuan() { //付款
    var that = this
    let params = {
      goods_id: this.data.id,
      pay_type: 1,
      is_deduction: this.data.flage ? 1 : 2,
      province: this.data.address.province ? this.data.address.province : '',
      city: this.data.address.city ? this.data.address.city : '',
      area: this.data.address.area ? this.data.address.area : '',
      address: this.data.address.address ? this.data.address.address : '',
      mobile: this.data.address.mobile ? this.data.address.mobile : '',
      realname: this.data.address.realname ? this.data.address.realname : '',
      remarks: this.data.content ? this.data.content : ''
    }
    wx.$http.get(wx.$get.add_goods_order,params,'支付...').then(res=>{
      if (res.code==0x0003) {
        wx.redirectTo({
          url: `/pages/myOrder/myOrder?type=${that.data.items.type}`
        })
    } else {
        let ress = res.data
        if(that.data.ios){
          wx.showToast({
            title: '抱歉,积分不足',
            icon: 'none',
            duration: 2000
          })
          that.quxiao(ress.order_id)
        }else{
          wx.requestPayment({
            timeStamp: ress.timeStamp,
            nonceStr: ress.nonceStr,
            package: ress.package,
            signType: 'MD5',
            paySign: ress.paySign,
            success(res) {
              wx.redirectTo({
                url: `/pages/myOrder/myOrder?type=${that.data.items.type}`
              })
            },
            fail(res) {
              that.quxiao(ress.order_id)
            }
          })
        }
    }
    that.my()
    })
  },
  my(){//我的
    var that = this
    var params = {
      
    }
    wx.$http.get(wx.$get.get_member_info,params).then(res=>{
      if(!res.vip_time){
        res.vip_time='1970-01-01'
      }
      console.log('我的', res)
      wx.$cache.set('userinfo',res)
    })
  },
  quxiao(id){
    var that = this
    var params ={
      order_id:id
    }
    wx.$http.get(wx.$get.cancel_goods_order,params).then(res=>{
      console.log('取消订单', res)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.$cache.get('dz')){
      this.setData({
        address:wx.$cache.get('dz'),
        jf:wx.$cache.get('userinfo').integral
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})